/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.pm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.pm.entity.XmglProject;
import org.springblade.pm.entity.ZTProduct;
import org.springblade.pm.entity.ZTProject;
import org.springblade.pm.entity.ZTProjectproduct;
import org.springblade.pm.mapper.XmglProjectMapper;
import org.springblade.pm.service.IXmglProjectService;
import org.springblade.pm.vo.MyPage;
import org.springblade.pm.vo.PageQuery;
import org.springblade.pm.vo.XmglProjectVo;
import org.springblade.pm.wrappper.XmglProjectWrapper;
import org.springblade.system.feign.ISysClient;
import org.springblade.system.user.entity.UserInfo;
import org.springblade.system.user.feign.IUserClient;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 服务实现类
 *
 * @author Chill
 */
@Service
@AllArgsConstructor
public class XmglProjectServiceImpl extends BaseServiceImpl<XmglProjectMapper, XmglProject> implements IXmglProjectService {

	XmglProjectMapper xmglProjectMapper;

	private IUserClient userClient;
	private ISysClient sysClient;

	@Override
	public List<Map<String, Object>> getLeader() {
		return xmglProjectMapper.getLeader();
	}

	@Override
	public List<XmglProject> getList(String deptid) {
		return baseMapper.getList(deptid);
	}

	/**
	 * 获取项目管理列表
	 *
	 * @param xmglProject
	 * @param pageQuery
	 * @return
	 */
	@Override
	public IPage<XmglProjectVo> getXmglList(XmglProject xmglProject, PageQuery pageQuery) {
		pageQuery.setCurrentIndex((pageQuery.getCurrent() - 1) * pageQuery.getSize());
		List<XmglProject> xmglProjectList = xmglProjectMapper.getXmglList(xmglProject, pageQuery);
		Page<XmglProjectVo> page = new MyPage<>();
		Map map = xmglProjectMapper.getListSumTotal(xmglProject);
		page.setTotal(Long.valueOf(map.get("sumTotal").toString()));
		page.setSize(pageQuery.getSize());
		List<XmglProjectVo> xmglProjectVoList = XmglProjectWrapper.build().entityVOList(xmglProjectList);
		page.setRecords(xmglProjectVoList);
		return page;
	}

	@Override
	public List<HashMap<String, Object>> getSelect(String type, String param) {
		List<HashMap<String, Object>> list = new ArrayList<>();
		HashMap<String, Object> hashMap = new HashMap<>();
		//下拉框类型
		hashMap.put("type", type);
		//下拉框参数
		hashMap.put("param", param);
		list = xmglProjectMapper.getSelect(hashMap);
		if ("leader".equals(type)) {
			R<UserInfo> userInfo;
			Iterator<HashMap<String, Object>> iterator = list.iterator();
			while (iterator.hasNext()) {
				HashMap<String, Object> stringObjectHashMap = iterator.next();
				if (stringObjectHashMap == null) {
					iterator.remove();
				} else {
					userInfo = userClient.userInfo(Long.valueOf(stringObjectHashMap.get("leader").toString()));
					if (userInfo.isSuccess()) {
						stringObjectHashMap.put("name", userInfo.getData().getUser().getRealName());
					} else {
						iterator.remove();
					}
				}
			}
		}
		return list;
	}

	/**
	 * 根据用户角色和部门获取项目里列表
	 *
	 * @param bladeUser
	 * @return
	 */
	@Override
	public List<HashMap<String, Object>> getProjectListByRoleAndDept(BladeUser bladeUser) {
		String roles = bladeUser.getRoleId();
		String[] roleArr = roles.split(",");
		List<String> roleList = Arrays.asList(roleArr);
		R<UserInfo> userInfo = userClient.userInfo(Long.valueOf(bladeUser.getUserId()));
		String userDeptId = null;
		if (userInfo.isSuccess()) {
			userDeptId = userInfo.getData().getUser().getDeptId();
		}
		HashMap<String, Object> map = new HashMap<>();
		if (roleList.contains("3")) {
			//总经理
			map.put("type", 3);
		} else if ((roleList.contains("8") && !roleList.contains("3"))||(roleList.contains("9") && !roleList.contains("8") && !roleList.contains("3"))) {
			//部门经理 和 项目经理
			map.put("type", 8);
			map.put("dept", userDeptId);
		}/* else if (roleList.contains("9") && !roleList.contains("8") && !roleList.contains("3")) {
			map.put("type", 9);
			map.put("userId", bladeUser.getUserId());
		}*/
		else {
			map.put("type", 3);
		}
		return xmglProjectMapper.getProjectListByRoleAndDept(map);
	}

	@Override
	public List<XmglProject> getXmlgIdByCode(String code) {
		QueryWrapper<XmglProject> wrapper = new QueryWrapper<>();
		wrapper.eq("code", code);
		return xmglProjectMapper.selectList(wrapper);

	}

	/**
	 * 根据项目类型获取项目编号
	 *
	 * @param projectCategory
	 * @return
	 */
	@Override
	public String getProjectCodeByProjectCategory(Integer projectCategory) {
		return xmglProjectMapper.getProjectCodeByProjectCategory(projectCategory);
	}

	/**
	 * 插入禅道项目表数据
	 *
	 * @param ztProject
	 * @return
	 */
	@Override
	public Boolean saveZT(ZTProject ztProject) {
		Integer result = xmglProjectMapper.insertZT(ztProject);
		if (null != result && result >= 1) {
			return true;
		}
		return false;
	}

	/**
	 * 修改禅道项目表数据
	 *
	 * @param ztProject
	 * @return
	 */
	@Override
	public Boolean updateZT(ZTProject ztProject) {
		Integer result = xmglProjectMapper.updateZT(ztProject);
		if (null != result && result >= 1) {
			return true;
		}
		return false;
	}


	/**
	 * 批量删除禅道数据库项目表
	 *
	 * @param codeList
	 * @returngetXmglList
	 */
	@Override
	public Boolean deleteBatchCodes(List<String> codeList) {
		Integer result = xmglProjectMapper.deleteBatchIds(codeList);
		if (null != result && result >= 1) {
			return true;
		}
		return false;
	}

	@Override
	public Boolean removeByIds(List<Integer> ids) {
		Integer result = xmglProjectMapper.removeByIds(ids);
		if (null != result && result >= 1) {
			return true;
		}
		return false;
	}

	@Override
	public Boolean saveZTProduct(ZTProduct ztProduct) {
		Integer result = xmglProjectMapper.insertZTProduct(ztProduct);
		if (null != result && result >= 1) {
			return true;
		}
		return false;
	}

	@Override
	public Boolean updateZTProduct(ZTProduct ztProduct) {
		Integer result = xmglProjectMapper.updateZTProduct(ztProduct);
		if (null != result && result >= 1) {
			return true;
		}
		return false;
	}

	@Override
	public Boolean saveZTProjectproduct(ZTProjectproduct ztProjectproduct) {
		Integer result = xmglProjectMapper.insertZTProjectproduct(ztProjectproduct);
		if (null != result && result >= 1) {
			return true;
		}
		return false;
	}

	@Override
	public Map<String, Integer> getBugList(String projectCode) {
		HashMap<String, Integer> bugMap = new HashMap<>();
		Map<String, Object> params = new HashMap<>();
		params.put("projectCode",projectCode);
		bugMap.put("bugCount",xmglProjectMapper.getBugList(params));
		params.put("severity",1);
		bugMap.put("oneCount",xmglProjectMapper.getBugList(params));
		params.put("severity",2);
		bugMap.put("twoCount",xmglProjectMapper.getBugList(params));
		params.put("severity",3);
		bugMap.put("threeCount",xmglProjectMapper.getBugList(params));
		params.put("severity",4);
		bugMap.put("fourCount",xmglProjectMapper.getBugList(params));
		return bugMap;
	}


}
